package cn.parus.controller;

import cn.parus.common.Execption.CustomException;
import cn.parus.common.Execption.Result;
import cn.parus.entity.User;
import cn.parus.entity.dto.UserLoginDTO;
import cn.parus.entity.dto.UserPasswordDTO;
import cn.parus.entity.dto.UserProfileDTO;
import cn.parus.entity.dto.UserRegisterDTO;
import cn.parus.entity.user.vo.UserProfileVO;
import cn.parus.service.UserService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: Axel
 * @Description: TODO 通用网络接口
 **/
@RestController
@RequestMapping("/")
public class WebController {

    @Autowired
    private UserService userService;

    /**
     * 登录
     * @param dto
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody @Valid UserLoginDTO dto) {
        // 调用服务层进行登录验证
        try {
            UserProfileVO user = userService.login(dto);
            return Result.success(user);
        }  catch (CustomException e) {
            return Result.error(e.getMessage());
        }

    }

    /**
     * 注册
     * @param dto
     * @return
     */
    @PostMapping("/register")
    public Result register(@RequestBody @Valid UserRegisterDTO dto) {
        try {
            User user = userService.register(dto);
            return Result.success(user);
        } catch (CustomException e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 修改密码
     * @param dto
     * @return
     */
    @PutMapping("/updatePassword")
    public Result updatePassword(@RequestBody UserPasswordDTO dto) {
        try {
            userService.updatePassword(dto);
            return Result.success("修改成功");
        } catch (CustomException e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 修改用户信息
     * @param userProfileDTO
     * @return
     */
    @PutMapping("/updateInfo")
    public Result updateInfo(@RequestBody UserProfileDTO userProfileDTO) {
        try {
            userService.updateInfo(userProfileDTO);
            return Result.success("修改成功");
        } catch (CustomException e) {
            return Result.error(e.getMessage());
        }
    }
}
